package dao.impl;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import dao.MessageDao;
import po.message.Message;

public class MessageDaoImpl implements MessageDao {
	private List<Message> message;
	public MessageDaoImpl() {
		message = new LinkedList<>();
	}
	@Override
	public boolean insert(Message t) {
		if (message.contains(t)) {
			return false;
		}
		return message.add(t);
	}
	@Override
	public boolean update(Message t) {
		int index = message.indexOf(t);
		if (index == -1) {
			return false;
		}else {
			message.set(index, t);
			return true;
		}
	}
	@Override
	public boolean delete(int id) {
		Iterator<Message> it = message.iterator();
		while (it.hasNext()) {
			Message mes = it.next();
			if (mes.getMsgId() == id) {
				it.remove();
				return true;
			}
		}
		return false;
	}
	@Override
	public Message selectById(int id) {
		for (Message m : message) {
			if (m.getMsgId() == id) {
				return m;
			}
		}
		return null;
	}
	@Override
	public List<Message> selectAll() {
		List<Message> copy = new LinkedList<>(message);
		return copy;
	}
	
}
